package com.hdu.mockService.utils;

import lombok.val;

import java.security.SecureRandom;
import java.util.Random;

public class RandomStringGenerator {

    private static final String UPPER_CASE_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final String LOWER_CASE_CHARS = "abcdefghijklmnopqrstuvwxyz";
    private static final String NUMERIC_CHARS = "0123456789";
    private static final Random RANDOM = new SecureRandom();

    /**
     * 生成指定长度范围内的随机字符串，可选包含大写字母、小写字母和数字。
     *
     * @param minLength        随机字符串的最小长度。
     * @param maxLength        随机字符串的最大长度。
     * @param includeUpperCase 是否包含大写字母。
     * @param includeLowerCase 是否包含小写字母。
     * @param includeNumeric   是否包含数字。
     * @return 生成的随机字符串。
     */
    public static String generateRandomString(int minLength, int maxLength,
                                              boolean includeUpperCase,
                                              boolean includeLowerCase,
                                              boolean includeNumeric) {
        if (minLength > maxLength) {
            throw new IllegalArgumentException("Minimum length cannot be greater than maximum length.");
        }
        if (!includeUpperCase && !includeLowerCase && !includeNumeric) {
            throw new IllegalArgumentException("At least one of uppercase, lowercase, or numeric characters must be included.");
        }

        val charPoolBuilder = new StringBuilder();
        if (includeUpperCase) charPoolBuilder.append(UPPER_CASE_CHARS);
        if (includeLowerCase) charPoolBuilder.append(LOWER_CASE_CHARS);
        if (includeNumeric) charPoolBuilder.append(NUMERIC_CHARS);

        val length = RANDOM.nextInt((maxLength - minLength) + 1) + minLength;
        val stringBuilder = new StringBuilder(length);

        for (int i = 0; i < length; i++) {
            stringBuilder.append(charPoolBuilder.charAt(RANDOM.nextInt(charPoolBuilder.length())));
        }

        return stringBuilder.toString();
    }

}
